package com.chagee.channel.server.controller.ocr;

import com.chagee.base.utils.HttpResponse;
import com.chagee.base.utils.HttpResponseUtil;
import com.chagee.channel.api.bo.ocr.*;
import com.chagee.channel.common.exception.BizErrorCode;
import com.chagee.channel.common.exception.BizException;
import com.chagee.channel.server.event.service.OcrService;
import com.chagee.channel.server.exception.ErrorCodeEnum;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ocr识别
 *
 * @author: chang.jiawei
 * @date: 2024/01/18
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/ocr")
public class OcrController {

    private final OcrService ocrService;

    /**
     * ocr识别食品经营许可证
     *
     * @param param
     * @return
     */
    @PostMapping("/foodOperationLicense")
    @ApiOperation("ocr识别食品经营许可证")
    @SuppressWarnings("unchecked")
    public HttpResponse<FoodOperationLicenseOcrDTO> ocrFoodOperationLicense(@RequestBody @Valid BaseFileUrlReq param) {
        FoodOperationLicenseOcrDTO result;
        try {
            result = ocrService.ocrFoodOperationLicense(param.getFileUrl());
        } catch (Exception e) {
            return HttpResponseUtil.failure(BizErrorCode.OCR_FAIL);
        }
        return HttpResponseUtil.success(result);
    }

    /**
     * ocr识别营业执照
     *
     * @param param
     * @return
     */
    @PostMapping("/businessLicense")
    @ApiOperation("ocr识别营业执照")
    @SuppressWarnings("unchecked")
    public HttpResponse<BusinessLicenseOcrDTO> ocrBusinessLicense(@RequestBody @Valid BaseFileUrlReq param) {
        BusinessLicenseOcrDTO result;
        try {
            result = ocrService.ocrBusinessLicense(param.getFileUrl());
        } catch (Exception e) {
            return HttpResponseUtil.failure(BizErrorCode.OCR_FAIL);
        }
        return HttpResponseUtil.success(result);
    }

    /**
     * ocr识别银行开户许可证
     *
     * @param param
     * @return
     */
    @PostMapping("/bankAccountLicense")
    @ApiOperation("ocr识别银行开户许可证")
    @SuppressWarnings("unchecked")
    public HttpResponse<BankAccountLicenseOcrDTO> ocrBankAccountLicense(@RequestBody @Valid BaseFileUrlReq param) {
        BankAccountLicenseOcrDTO result;
        try {
            result = ocrService.ocrBankAccountLicense(param.getFileUrl());
        } catch (Exception e) {
            return HttpResponseUtil.failure(BizErrorCode.OCR_FAIL);
        }
        return HttpResponseUtil.success(result);
    }

    /**
     * ocr识别身份证正反面
     *
     * @return
     */
    @PostMapping("/idCard")
    @ApiOperation("ocr识别身份证正反面")
    public HttpResponse<IdCardDTO> ocrIdCard(@RequestBody IdCardReq req) {
        try {
            IdCardFrontDTO idCardFrontDTO = ocrService.ocrIdCardFront(req.getFrontUrl());
            IdCardBackDTO idCardBackDTO = ocrService.ocrIdCardBack(req.getBackUrl());
            IdCardDTO idCardDTO = new IdCardDTO(idCardFrontDTO, idCardBackDTO);
            return HttpResponseUtil.success(idCardDTO);
        } catch (Exception e) {
            log.error("ocr识别身份证正反面异常", e);
            return HttpResponseUtil.failure(new BizException(
                ErrorCodeEnum.IDCARD_OCR_ERROR.getValue(), ErrorCodeEnum.IDCARD_OCR_ERROR.getMessage()));
        }
    }

    /**
     * ocr识别身份证正反面
     *
     * @return
     */
    @PostMapping("/idCard/front")
    @ApiOperation("ocr识别身份证正面")
    public HttpResponse<IdCardFrontDTO> ocrIdCardFront(@RequestBody @Valid BaseFileUrlReq param) {
        try {
            IdCardFrontDTO idCardFrontDTO = ocrService.ocrIdCardFront(param.getFileUrl());
            return HttpResponseUtil.success(idCardFrontDTO);
        } catch (Exception e) {
            log.error("ocr识别身份证正反面异常", e);
            return HttpResponseUtil.failure(new BizException(
                ErrorCodeEnum.IDCARD_OCR_ERROR.getValue(), ErrorCodeEnum.IDCARD_OCR_ERROR.getMessage()));
        }
    }

    /**
     * ocr识别身份证正反面
     *
     * @return
     */
    @PostMapping("/idCard/back")
    @ApiOperation("ocr识别身份证反面")
    public HttpResponse<IdCardBackDTO> ocrIdCardBack(@RequestBody @Valid BaseFileUrlReq param) {
        try {
            IdCardBackDTO backDTO = ocrService.ocrIdCardBack(param.getFileUrl());
            return HttpResponseUtil.success(backDTO);
        } catch (Exception e) {
            log.error("ocr识别身份证正反面异常", e);
            return HttpResponseUtil.failure(new BizException(
                ErrorCodeEnum.IDCARD_OCR_ERROR.getValue(), ErrorCodeEnum.IDCARD_OCR_ERROR.getMessage()));
        }
    }

    /**
     * ocr识别银行卡
     *
     * @return
     */
    @PostMapping("/bankCard")
    @ApiOperation("ocr识别银行卡")
    public HttpResponse<BankCardDTO> ocrBankCard(@RequestBody @Valid BaseFileUrlReq param) {
        BankCardDTO result;
        try {
            result = ocrService.ocrBankCard(param.getFileUrl());
        } catch (Exception e) {
            return HttpResponseUtil.failure(BizErrorCode.OCR_FAIL);
        }
        return HttpResponseUtil.success(result);
    }

    /**
     * ocr识别银行卡
     *
     * @return
     */
    @PostMapping("/businessLicenseHK")
    @ApiOperation("ocr识别香港营业执照")
    public HttpResponse<HKBusinessLicenseOcrDTO> ocrHKBusinessLicense(@RequestBody @Valid BaseFileUrlReq param) {
        HKBusinessLicenseOcrDTO result;
        try {
            result = ocrService.ocrHKBusinessLicense(param.getFileUrl());
        } catch (Exception e) {
            return HttpResponseUtil.failure(BizErrorCode.OCR_FAIL);
        }
        return HttpResponseUtil.success(result);
    }

}
